package com.tecklab.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tecklab.demo.common.BizException;
import com.tecklab.demo.dto.DepartmentDTO;
import com.tecklab.demo.dto.EmployeeDTO;
import com.tecklab.demo.entity.DepartmentEntity;
import com.tecklab.demo.entity.HospitalEntity;
import com.tecklab.demo.mapper.DepartmentMapper;
import com.tecklab.demo.mapper.HospitalMapper;
import com.tecklab.demo.param.DepartmentAddParam;
import com.tecklab.demo.param.DepartmentModifyParam;
import com.tecklab.demo.service.DepartmentService;
import com.tecklab.demo.service.EmployeeService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * * 用户服务实现类
 */
@Service  // 重要：添加@Service注解 , 标记为Spring服务组件
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private EmployeeService employeeService;

    @Resource
    private HospitalMapper hospitalMapper;

    //@Override明确表示这是接口方法的实现
    @Override
    public int create(DepartmentAddParam addParam) {
        //department对象本身无需判空（因为是 new出来的）
        DepartmentEntity department = new DepartmentEntity();
        if (StringUtils.isBlank(addParam.getName())) {
            throw new BizException("部门名字不能为空");
        }
        department.setName(addParam.getName());
        //医院id为数据库的主键无需判空
        department.setHospitalId(addParam.getHospitalId());

        return departmentMapper.insert(department);

    }

    @Override
    public int modify(DepartmentModifyParam addParam) {
        DepartmentEntity department = new DepartmentEntity();
        if (StringUtils.isBlank(addParam.getName())) {
            throw new BizException("部门名字不能为空");
        }
        department.setName(addParam.getName());
        department.setId(addParam.getId());
        department.setHospitalId(addParam.getHospitalId());
        return departmentMapper.updateById(department);
    }

    @SneakyThrows
    private DepartmentEntity convertDto2Entity(DepartmentDTO dto) {
        DepartmentEntity departmentEntity = new DepartmentEntity();
        BeanUtils.copyProperties(departmentEntity, dto);
        return departmentEntity;
    }

    @SneakyThrows
    private DepartmentDTO convertEntity2Dto(DepartmentEntity entity) {
        DepartmentDTO dto = new DepartmentDTO();
        BeanUtils.copyProperties(dto, entity);
        return dto;
    }

    @Override
    public DepartmentDTO getById(Integer id) {
        //根据ID查询部门信息
        DepartmentEntity department = departmentMapper.selectById(id);
        if (department == null) {
            throw new BizException("部门不存在");
        }
        DepartmentDTO departmentDTO = this.convertEntity2Dto(department);

        //  获取医院名称,???
//        if (department.getHospitalId() != null) {
//            HospitalDTO hospitalDTO = hospitalService.getById(department.getHospitalId());
//            departmentDTO.setHospitalName(hospitalDTO.getName());
//        }
        if (department.getHospitalId() != null) {
            HospitalEntity hospital = hospitalMapper.selectById(department.getHospitalId());
            if (hospital != null) {
                departmentDTO.setHospitalName(hospital.getName());
            }
        }
        // List<EmployeeDTO> employeeDTOList = employeeService.getByDepartmentId(id);
        //departmentDTO.setEmployeeDTOList(employeeDTOList);
        //部门id为内部必有，无需判空
        List<EmployeeDTO> employeeDTOList = employeeService.getByDepartmentId(id);
        // 5. 为每个员工设置部门名称（新增部分）
        for (EmployeeDTO employee : employeeDTOList) {
            employee.setDepartmentName(department.getName());
        }
        departmentDTO.setEmployeeDTOList(employeeDTOList);
        // 查询员工列表时直接设置部门名称,???
//        List<EmployeeDTO> employeeDTOList = employeeService.getByDepartmentId(id)
//                .stream()
//                .peek(e -> e.setDepartmentName(department.getName())) // 关键设置
//                .collect(Collectors.toList());
//        departmentDTO.setEmployeeDTOList(employeeDTOList);
        return departmentDTO;
    }

    @Override
    //根据部门名称获取部门信息
    public DepartmentDTO getByName(String name) {
        //1. 构建查询条件
        LambdaQueryWrapper<DepartmentEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DepartmentEntity::getName, name);
        //2. 执行查询
        DepartmentEntity departmentEntity = departmentMapper.selectOne(lambdaQueryWrapper);
        //3. 转换并返回
        return this.convertEntity2Dto(departmentEntity);
    }

    @Override
    //根据医院ID获取部门信息
    public List<DepartmentDTO> getByHospitalId(Integer hospitalId) {
        //1. 构建查询条件
        LambdaQueryWrapper<DepartmentEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DepartmentEntity::getHospitalId, hospitalId);
        //2. 执行查询
        List<DepartmentEntity> entityList = departmentMapper.selectList(lambdaQueryWrapper);
        //3. 转换列表
        List<DepartmentDTO> departmentDTOList = new ArrayList<>();
        for (int i = 0; i < entityList.size(); i++) {
            entityList.get(i);
            departmentDTOList.add(this.convertEntity2Dto(entityList.get(i)));
        }
        return departmentDTOList;
    }

}
